﻿using Bll;
using Help;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;

namespace Myproject.BSM.Ashx
{
    /// <summary>
    /// SendEmail 的摘要说明
    /// </summary>
    public class SendEmail : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string logName = context.Request.Params["userLoginName"];
            string email = context.Request.Params["email"];
            if (!new AdminBLL().ExistsByAdminName(logName))
            {
                context.Response.Write(ReturnJsonHelp.OutHandlerJson(false, "账号不存在"));
            }
            else
            {
                DataRow dr = new AdminBLL().GetList(" AdminName='" + logName + "' ").Tables[0].Rows[0];
                if (dr["Email"].ToString() != email)
                {
                    context.Response.Write(ReturnJsonHelp.OutHandlerJson(false, "绑定邮箱错误"));
                }
                else
                {

                    try
                    {
                        #region 发送邮件
                        int number;
                        string strCode = string.Empty;
                        //随机数种子
                        Random random = new Random();

                        for (int i = 0; i < 6; i++) //校验码长度为4
                        {
                            //随机的整数
                            number = random.Next();

                            //字符从0-9,A-Z中随机产生,对应的ASCII码分别为
                            //48-57,65-90
                            number = number % 36;
                            if (number < 10)
                            {
                                number += 48;
                            }
                            else
                            {
                                number += 55;
                            }
                            strCode += ((char)number).ToString();
                        }
                        string newPwd = strCode;

                        MailMessage mailObj = new MailMessage();

                        mailObj.From = new MailAddress("szcs890621@163.com"); //发送人邮箱地址
                        mailObj.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
                        mailObj.IsBodyHtml = true;//设置为HTML格式
                        mailObj.Priority = MailPriority.High;//优先级
                        mailObj.To.Add(dr["Email"].ToString());   //收件人邮箱地址
                        mailObj.Subject = "密码找回";    //主题
                        string textHtml = "【系统邮件请勿回复】新密码：" + newPwd;
                        mailObj.Body = textHtml;     //正文
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.163.com";        //smtp服务器名称
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials = new NetworkCredential("szcs890621@163.com", "asd123456");   //发送人的登录名和密码
                        smtp.Send(mailObj);

                        //更新密码
                        Model.AdminModel model = new AdminBLL().GetModelByname(logName);
                        model.AdminPwd = newPwd;
                        new AdminBLL().Update(model);
                        #endregion
                        context.Response.Write(ReturnJsonHelp.OutHandlerJson(true, "发送成功"));
                    }
                    catch (Exception)
                    {
                        context.Response.Write(ReturnJsonHelp.OutHandlerJson(false, "发送失败"));
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}